﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[spInsertaUsuario]
	(@pnIdUsuario INT OUT,
	@pnIdLaboratorio INT,
	@psUserName	NVARCHAR(20),
	@pnIdSucursal INT
	)
AS
BEGIN
	DECLARE @nUserName NVARCHAR(20),
			@nPerfilDoc INT
	SET @nPerfilDoc = 3
	---- Create the variables for the random number generation
	--DECLARE @Random INT;
	--DECLARE @Upper INT;
	--DECLARE @Lower INT
	------ This will create a random number between 1 and 999
	--SET @Lower = 1000000 ---- The lowest random number
	--SET @Upper = 9999999 ---- The highest random number
	--SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
	
	DECLARE @KEYID UNIQUEIDENTIFIER
	DECLARE @nPass NVARCHAR(200)
	SET @KEYID = KEY_GUID('SYM_KEY')
	 
	OPEN SYMMETRIC KEY SYM_KEY
	DECRYPTION BY PASSWORD='##CLAB##'
 
	SELECT @nPass = ENCRYPTBYKEY(@KEYID,@psUserName)
	--SELECT @nPass
	--SELECT CAST(DECRYPTBYKEY(@nPass) AS VARCHAR(200))
	CLOSE SYMMETRIC KEY SYM_KEY
	
	--EXEC spObtenerFolio 2, @pnIdUsuario OUT;
	EXEC dbo.spSequence	@pnIdLaboratorio = 1, @pnIdSucursal = NULL, @psTabla = 'CatUsuario', @psCampoFolio = 'IdUsuario', @pnNextValue = @pnIdUsuario OUT
	
	INSERT INTO dbo.CatUsuario(IdUsuario, IdLaboratorio, UserName, [Password], IdEstatus, IdSucursal)
	VALUES(@pnIdUsuario, @pnIdLaboratorio, @psUserName, @nPass, 1, @pnIdSucursal)
	
END